require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'

class TC_AccountSetting_test < Test::Unit::TestCase
   
    def startup
      $Message=  'Hello I was wondering if we can get connected'
      $RegExp = '^[a-zA-Z0-9_\- @\.]+$'
      $DefaultFriends = 'admin'
      $RegExpPattern = '^.*$'
      $DefaultFriends = 'admin'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        ie.button(:id, /loginButton$/).click
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToMembershipAdministration
        ie.goto($SITE_URL + 'controlpanel/membership/')
        assert(ie.contains_text('Membership Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Membership Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
    def test_10_navigateToAccountSetting 
        ie.goto($SITE_URL +'/controlpanel/membership/AccountSettings.aspx')
        assert(ie.radio(:id,/optAllowLogin_0$/).exists?)
        assert(ie.radio(:id,/optAllowLogin_1$/).exists?)
        ie.radio(:id,/optAllowLogin_0$/).set
        assert(ie.radio(:id,/optEnableBannedUsersToLogin_0$/).exists?)
        assert(ie.radio(:id,/optEnableBannedUsersToLogin_1$/).exists?)
        ie.radio(:id,/optEnableBannedUsersToLogin_1$/).set
        assert(ie.radio(:id,/optAllowNewUserRegistration_0$/).exists?)
        assert(ie.radio(:id,/optAllowNewUserRegistration_1$/).exists?)
        ie.radio(:id,/optAllowNewUserRegistration_0$/).set
        assert(ie.radio(:id,/optShowContactCheckboxes_0$/).exists?)
        assert(ie.radio(:id,/optShowContactCheckboxes_1$/).exists?)
        ie.radio(:id,/optShowContactCheckboxes_1$/).set
        assert(ie.select_list(:id,/ddlNewUserModerationLevel$/).exists?)
        ie.select_list(:id,/ddlNewUserModerationLevel$/).select('User is Moderated')
        assert(ie.text_field(:id,/txtUserNameRegEx$/).exists?)
        ie.text_field(:id,/txtUserNameRegEx$/).set($RegExp)
        assert(ie.text_field(:id,/txtUsernameMinLength$/).exists?)
        ie.text_field(:id,/txtUsernameMinLength$/).set('5')
        assert(ie.text_field(:id,/txtUsernameMaxLength$/).exists?)
        ie.text_field(:id,/txtUsernameMaxLength$/).set('60')
        assert(ie.radio(:id,/optAccountActivation_0$/).exists?)
        assert(ie.radio(:id,/optAccountActivation_1$/).exists?)
        assert(ie.radio(:id,/optAccountActivation_2$/).exists?)
        assert(ie.radio(:id,/optAccountActivation_3$/).exists?)
        ie.radio(:id,/optAllowNewUserRegistration_0$/).set
        assert(ie.radio(:id,/optPasswordRecovery_0$/).exists?)
        assert(ie.radio(:id,/optPasswordRecovery_1$/).exists?)
        ie.radio(:id,/optPasswordRecovery_0$/).set
        assert(ie.text_field(:id,/txtPasswordRegEx$/).exists?)
        ie.text_field(:id,/txtPasswordRegEx$/).set($RegExpPattern)
        sleep 2
        # Profile Settings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01 .SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01 .GetTabAtIndex(1))")
        assert(ie.radio(:id,/optAllowSignatures_0$/).exists?)
        assert(ie.radio(:id,/optAllowSignatures_1$/).exists?)
        ie.radio(:id,/optAllowSignatures_0$/).set
        assert(ie.radio(:id,/optEnableSignatures_0$/).exists?)
        assert(ie.radio(:id,/optEnableSignatures_1$/).exists?)
        ie.radio(:id,/optEnableSignatures_0$/).set
        assert(ie.text_field(:id,/txtSignatureMaxLength$/).exists?)
        ie.text_field(:id,/txtSignatureMaxLength$/).set('60')
        assert(ie.radio(:id,/optAllowGender_0$/).exists?)
        assert(ie.radio(:id,/optAllowGender_1$/).exists?)
        ie.radio(:id,/optAllowGender_0$/).set
        assert(ie.radio(:id,/optRequireAuthenticationForProfileViewing_0$/).exists?)
        assert(ie.radio(:id,/optRequireAuthenticationForProfileViewing_1$/).exists?)
        ie.radio(:id,/optRequireAuthenticationForProfileViewing_1$/).set
        assert(ie.radio(:id,/DisplayNames_0$/).exists?)
        assert(ie.radio(:id,/DisplayNames_1$/).exists?)
        ie.radio(:id,/DisplayNames_0$/).set
        sleep 2
        # Avatar Settings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01 .SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01 .GetTabAtIndex(2))")
        assert(ie.radio(:id,/optEnableAvatars_0$/).exists?)
        assert(ie.radio(:id,/optEnableAvatars_1$/).exists?)
        ie.radio(:id,/optEnableAvatars_0$/).set
        assert(ie.radio(:id,/optEnableRemoteAvatars_0$/).exists?)
        assert(ie.radio(:id,/optEnableRemoteAvatars_1$/).exists?)
        ie.radio(:id,/optEnableRemoteAvatars_0$/).set
        assert(ie.text_field(:id,/txtAvatarHeight$/).exists?)
        ie.text_field(:id,/txtAvatarHeight$/).set('120')
        assert(ie.text_field(:id,/txtAvatarWidth$/).exists?)
        ie.text_field(:id,/txtAvatarWidth$/).set('60')
        sleep 2
        # Friendship Settings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01 .SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01 .GetTabAtIndex(3))")
        sleep 3
        #~ ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_txtDefaultFriends_ctl00').execCommand('mceSetContent', false,'"+$DefaultFriends+"')")
        assert(ie.text_field(:id,/txtDefaultFriends_ctl00$/).exists?)
        sleep 5 
        ie.text_field(:id,/txtDefaultFriends_ctl00$/).value= $DefaultFriends 
        sleep 5
        assert(ie.radio(:id,/optRequireFriendRequestMessage_0$/).exists?)
        assert(ie.radio(:id,/optRequireFriendRequestMessage_1$/).exists?)
        ie.radio(:id,/optRequireFriendRequestMessage_0$/).set
        sleep 5
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_txtDefaultFriendRequestMessage_ctl00_Editor').execCommand('mceSetContent', false,'"+$Message+"')")
        sleep 5
        assert(ie.link(:id,/btnSave$/).exists?)
        #ie.link(:id,/btnSave$/).click
    end
      
    def test_99_closeIE
      ie.close
    end
    
   
end